<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-09-11
 * Time: 下午 5:11
 * 工具类
 */
namespace Pub\tool;
class Tool
{
	/**
	 * [createCode 生成系统唯一标识符8位]
	 * @return [type] [系统唯一标识符 字母+数字]
	 */
	static function createCode()
	{
		return sprintf('%x',crc32(microtime()));
	}

	/**
	 * 创建订单编号
	 */
	static function createOrder()
	{
		return TIMESTAMP.mt_rand(10000,99999);
	}

	/**
	 * [GetIp 获取用户客户端真实IP]
	 */
	static public function getIp(){
		$realip = '';
		$unknown = 'unknown';
		if (isset($_SERVER)){
			if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){
				$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
				foreach($arr as $ip){
					$ip = trim($ip);
					if ($ip != 'unknown'){
						$realip = $ip;
						break;
					}
				}
			}else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){
				$realip = $_SERVER['HTTP_CLIENT_IP'];
			}else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){
				$realip = $_SERVER['REMOTE_ADDR'];
			}else{
				$realip = $unknown;
			}
		}else{
			if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){
				$realip = getenv("HTTP_X_FORWARDED_FOR");
			}else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){
				$realip = getenv("HTTP_CLIENT_IP");
			}else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){
				$realip = getenv("REMOTE_ADDR");
			}else{
				$realip = $unknown;
			}
		}
		$realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;
		return $realip;
	}

    /**
     * 解析单页应用router
     * @param $_url 需要解析的链接
     * @param $_route  是否解析route
     * @return String $_url
     */
	public static function parseRouter($_url,$_route = false)
    {
        global $_GPC;
        if(strstr($_url,'&wxref=mp.weixin.qq.com')){
            $_url = str_replace('&wxref=mp.weixin.qq.com','',$_url);
        }
        if(isset($_GPC['route']) && $_route){
            $_url = urldecode($_url);
        }
        return $_url;
    }
}